/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package monopoly.results;

import GameEvents.Event;
import GameEvents.GameEvents;
import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 *
 * @author blecherl
 */
public class EventArrayResult extends MonopolyResult {

    private static final Event[] EMPTY_RESULTS = new Event[0];
    private Event[] results;
    private ArrayList<Event> updateArray;

    public EventArrayResult(Event... results) {
	super();
	this.results = results;
	this.updateArray = new ArrayList<Event>();
	for (int i = 0; i < results.length; ++i) {
	    updateArray.add(results[i]);
	}
    }

    public EventArrayResult(String errorMessage) {
	super(errorMessage);
    }

    public EventArrayResult(boolean isSuccessfull, String errorMessage, Event[] results) {
	super(isSuccessfull, errorMessage);
	this.results = results;
    }

    public  Event[] getResults() {

	    return results == null ? EMPTY_RESULTS : results;

    }

    /**
     * Gets the Event From the Requested Place
     * @param eventID The Requested Event ID in the Array
     * @return The Event Requested
     */
    public Event getEvent(Integer eventID) {

	    if (results.length > eventID) {
		// Return the Event in the Place of Event ID
		return results[eventID];
	    } else {
		// No Event found - Event ID is Bigger than the Events Array
		return null;
	    }

    }

    public void setResults(Event[] NewResults) {

	try {
	    if (NewResults == null) {
		results = EMPTY_RESULTS;
		updateArray.clear();
	    } else {
		results = NewResults;
		updateArray.clear();
		for (int i = 0; i < results.length; ++i) {
		    updateArray.add(results[i]);
		}
	    }
	} finally {
	    
	}
    }

    //add new Event to the end of list
    public synchronized void addResults(Event param) {
	// Update the ArrayList
	updateArray.add(param);
	// Copy all the Events
	// Create a New Copy Array
	Event[] copyResults = new Event[updateArray.size()]; // Create a new Return array
	try {
	    // Copy the Events to the New Events Array
	    for (int i = 0; i < updateArray.size(); ++i) {
		// Add the Cloned Event
		copyResults[i] = updateArray.get(i);
	    }
	} finally {
	    // Replace the Current Array with the New Copy Array
	    results = copyResults;
	}
    }

    public Integer getSize() {
	    if (results != null) {
		return results.length;
	    } else {
		results = EMPTY_RESULTS;
		return 0;
	    }
    }

    public static EventArrayResult error(String message) {
	return new EventArrayResult(message);
    }

    public  Event getLastEvent() {

	    // Verify that the Results Array is not Null
	    if (results != null) {
		// Verify that we haveany Events
		if (results.length > 0) {
		    //return the last event occured
		    return results[results.length - 1];
		} else {
		    System.out.println("Events Array is EMPTY");
		    // The Resuls Array is Empty
		    return new GameEvents();
		}
	    } else {
		System.out.println("Events Array is NULL");
		// This should never Happen
		return new GameEvents();
	    }

    }
}
